<template>
  <div>
    <div class="i-layout-page-header">
      <PageHeader
        class="product_tabs"
        title="卡包列表"
        hidden-breadcrumb
      ></PageHeader>
    </div>
    <Card :bordered="false" dis-hover class="ivu-mt">
      <Form
        ref="tableFrom"
        :model="tableFrom"
        :label-width="labelWidth"
        :label-position="labelPosition"
        @submit.native.prevent
      >
        <Row type="flex" :gutter="24">
          <Col v-bind="grid">
            <FormItem label="是否有效：" label-for="is_show">
              <Select
                v-model="tableFrom.is_show"
                placeholder="请选择"
                clearable
                @on-change="userSearchs"
              >
                <Option value="1">正常</Option>
                <Option value="0">未开启</Option>
              </Select>
            </FormItem>
          </Col>
          <!-- <Col v-bind="grid">
            <FormItem label="类型：" label-for="is_show">
              <Select
                v-model="issue_type"
                placeholder="请选择"
                clearable
                @on-change="userSearchs"
              >
                <Option value="all">全部</Option>
                <Option value="1">手动领取</Option>
                <Option value="2">新人券</Option>
                <Option value="3">赠送券</Option>
                <Option value="4">会员券</Option>
              </Select>
            </FormItem>
          </Col> -->
          <Col v-bind="grid">
            <FormItem label="卡包名称：" label-for="title">
              <Input
                search
                enter-button
                v-model="tableFrom.title"
                placeholder="请输入卡包名称"
                @on-search="userSearchs"
              />
            </FormItem>
          </Col>
        </Row>
        <Row type="flex">
          <Col v-bind="grid">
            <Button
              v-auth="['admin-marketing-store_coupon-add']"
              type="primary"
              icon="md-add"
              @click="add"
              >添加卡包</Button
            >
          </Col>
        </Row>
      </Form>
      <Table
        :columns="columns1"
        :data="tableList"
        ref="table"
        class="mt25"
        :loading="loading"
        highlight-row
        no-userFrom-text="暂无数据"
        no-filtered-userFrom-text="暂无筛选结果"
      >
        <template slot-scope="{ row }" slot="type">
          <span v-if="row.type == 1">闺蜜卡包</span>
          <span v-else-if="row.type == 2">权益卡包</span>
          <span v-else-if="row.type == 3">抽奖卡包</span>
          <span v-else-if="row.type == 4">商品卡包</span>
          <span v-else-if="row.type == 5">生日卡包</span>
          <span v-else-if="row.type == 6">年终回馈</span>
          <span v-else-if="row.type == 7">手动领取</span>
        </template>
        <template slot-scope="{ row }" slot="level">
          <div v-if="row.type == 2 || row.type == 5">
            <div v-for="(item,index) in row.level" :key="index">
              <span v-if="item == 1">普通会员</span>
              <span v-if="item == 2">银卡</span>
              <span v-if="item == 3">金卡</span>
              <span v-if="item == 4">铂金卡</span>
              <span v-if="item == 5">钻石卡</span>
              <span v-if="item == 6">翡翠卡</span>
              <span v-if="item == 7">V卡</span>
            </div>
          </div>
          <div v-if="row.type == 6">
            <div v-for="(item,index) in row.level" :key="index">
              <span v-if="item == 1">2000-4999</span>
              <span v-if="item == 2">5000-9999</span>
              <span v-if="item == 3">10000-29999</span>
              <span v-if="item == 4">30000-59999</span>
              <span v-if="item == 5">60000-99999</span>
              <span v-if="item == 6">100000-</span>
            </div>
          </div>
        </template>

        <template slot-scope="{ row }" slot="begin_time">
          <div v-if="row.use_day == 0">
            {{ row.begin_time}} - {{ row.end_time}}
          </div>
          <div v-else>
            {{ row.use_day }}
          </div>
        </template>
        <template slot-scope="{ row }" slot="is_show">
          <i-switch
            v-model="row.is_show"
            :value="row.is_show"
            :true-value="1"
            :false-value="0"
            size="large"
            @on-change="openChange(row)"
          >
            <span slot="open">开启</span>
            <span slot="close">关闭</span>
          </i-switch>
        </template>
        <template slot-scope="{ row, index }" slot="action">
          <!-- <a @click="receive(row)">领取记录</a> -->
          <a @click="copy(row)">复制</a>
          <Divider type="vertical" />
          <a @click="editor(row)">编辑</a>
          <Divider type="vertical" />
          <a @click="couponDel(row, '删除发布的卡包', index)">删除</a>
        </template>
      </Table>
      <div class="acea-row row-right page">
        <!-- <Page
          :total="total"
          :current="tableFrom.page"
          show-elevator
          show-total
          @on-change="pageChange"
          :page-size="tableFrom.limit"
        /> -->
         <Page
          :total="tableFrom.total"
          :current="tableFrom.pageNum"
          show-elevator
          show-total
          @on-change="pageChange"
          :page-size="tableFrom.pageSize"
          @on-page-size-change="limitChange"
          />
      </div>
    </Card>
    <!-- 领取记录 -->
    <Modal
      v-model="modals2"
      scrollable
      footer-hide
      closable
      title="领取记录"
      :mask-closable="false"
      width="700"
    >
      <Table
        :columns="columns2"
        :data="receiveList"
        ref="table"
        class="mt25"
        :loading="loading2"
        highlight-row
        no-userFrom-text="暂无数据"
        no-filtered-userFrom-text="暂无筛选结果"
      >
        <template slot-scope="{ row, index }" slot="avatar">
          <viewer>
            <div class="tabBox_img">
              <img v-lazy="row.avatar" />
            </div>
          </viewer>
        </template>
      </Table>
      <div class="acea-row row-right page">
        <Page
          :total="total2"
          show-elevator
          show-total
          @on-change="receivePageChange"
          :page-size="receiveFrom.limit"
        />

      </div>
    </Modal>
  </div>
</template>

<script>
import { mapState } from "vuex";
import {
  releasedissueLogApi,
  cardListApi,
  couponsetShowApi,
  cardpackReleased
} from "@/api/marketing";
export default {
  name: "storePackage",
  data() {
    return {
      modals2: false,
      grid: {
        xl: 7,
        lg: 7,
        md: 12,
        sm: 24,
        xs: 24,
      },
      loading: false,
      columns1: [
        {
          title: "ID",
          key: "id",
          width: 80,
        },
        {
          title: "卡包名称",
          key: "title",
          minWidth: 120,
        },
        {
          title: "卡包类型",
          slot: "type",
          minWidth: 80,
        },
        {
          title: "使用时间段/天数",
          slot: "begin_time",
          minWidth: 250,
          align: "center"
        },
        {
          title: "卡包等级",
          slot: "level",
          minWidth: 130,
        },
        {
          title: "是否开启",
          slot: "is_show",
          minWidth: 90,
        },
        {
          title: "操作",
          slot: "action",
          fixed: "right",
          minWidth: 200,
        },
      ],
      // 底部分页
      tableFrom: {
        is_show: "",
        title: "",
        issue_type: "",
        page: 1,
        limit: 10,
        total: 0, // 总条数
        pageNum: 1, // 当前页
        pageSize: 10, // 每页显示条数
      },
      issue_type: "",
      tableList: [],
      total: 0,
      FromData: null,
      receiveList: [],
      loading2: false,
      columns2: [
        {
          title: "用户名",
          key: "nickname",
          minWidth: 150,
        },
        {
          title: "用户头像",
          slot: "avatar",
          minWidth: 100,
        },
        {
          title: "领取时间",
          key: "add_time",
          minWidth: 140,
        },
      ],
      total2: 0,
      receiveFrom: {
        page: 1,
        limit: 10,
      },
      rows: {},
    };
  },
  created() {
    this.getList();
  },
  computed: {
    ...mapState("admin/layout", ["isMobile"]),
    labelWidth() {
      return this.isMobile ? undefined : 90;
    },
    labelPosition() {
      return this.isMobile ? "top" : "left";
    },
  },
  methods: {
    // 失效
    // couponInvalid(row, tit, num) {
    //   this.delfromData = {
    //     title: tit,
    //     num: num,
    //     url: `marketing/coupon/status/${row.id}`,
    //     method: "PUT",
    //     ids: "",
    //   };
    //   this.$refs.modelSure.modals = true;
    // },
    // 领取记录
    receive(row) {
      this.modals2 = true;
      this.rows = row;
      this.getReceivelist(row);
    },
    getReceivelist(row) {
      this.loading2 = true;
      releasedissueLogApi(row.id, this.receiveFrom)
        .then(async (res) => {
          let data = res.data;
          this.receiveList = data.list;
          this.loading2 = false;
        })
        .catch((res) => {
          this.loading2 = false;
          this.$Message.error(res.msg);
        });
    },
    // 领取记录改变分页
    receivePageChange(index) {
      this.receiveFrom.page = index;
      this.getReceivelist(this.rows);
    },
    // 删除
    couponDel(row, tit, num) {
      let delfromData = {
        title: tit,
        num: num,
        success: cardpackReleased(row.id),
      };
      this.$modalSure(delfromData)
        .then((res) => {
          this.$Message.success(res.msg);
          this.tableList.splice(num, 1);
        })
        .catch((res) => {
          this.$Message.error(res.msg);
        });
    },
    // 列表
    getList() {
      this.loading = true;
      this.tableFrom.issue_type = this.issue_type === "all" ? "" : this.issue_type;
      this.tableFrom.is_show = this.tableFrom.is_show || "";
      cardListApi(this.tableFrom)
        .then(async (res) => {
          let data = res.data;
          this.tableList = data.list;
          this.tableList = this.tableList.map((item) => {
              item.level = item.level.split(',').map(Number);
              return item;
          });
          console.log(this.tableList,"123465")
          this.loading = false;
          this.tableFrom.total = data.count;
        })
        .catch((res) => {
          this.loading = false;
          this.$Message.error(res.msg);
        });
    },
    pageChange(index) {
      this.tableFrom.page = index;
      this.getList();
    },
    limitChange(limit) {
      this.tableFrom.pageSize = limit;
      this.getList();
    },
    // 表格搜索
    userSearchs() {
      this.tableFrom.page = 1;
      this.getList();
    },
    // 添加卡包
    add() {
      this.$router.push({ path: "/admin/marketing/store_package/create"});
    },
    // 修改
    editor(data) {
      this.$router.push({
        path: `/admin/marketing/store_package/create/${data.id}`,
        query: { type: 'edit' } // 增加修改标识
      });
    },
    // 复制
    copy(data) {
      this.$router.push({
        path: `/admin/marketing/store_package/create/${data.id}`,
        query: { type: 'copy' } // 增加复制标识
      });
    },
    // 是否开启
    openChange(data) {
      couponsetShowApi(data).then(() => this.getList());
    },
  },
};
</script>

<style scoped lang="stylus">
.fa
    color #0a6aa1
    display block
.sheng
   color #ff0000
   display block
.tabBox_img
    width 36px
    height 36px
    border-radius:4px
    cursor pointer
    img
        width 100%
        height 100%
</style>
